// 具体公文
var doc_id = 0

$(document).ready(function () {


    //获得参数的方法
    var request =
    {
        QueryString : function(val)
        {
            var uri = window.location.search;
            var re = new RegExp("" +val+ "=([^&?]*)", "ig");
            return ((uri.match(re))?(uri.match(re)[0].substr(val.length+1)):null);
        }
    };

    doc_id = request.QueryString("doc_id");

    // 查看是否具有相关权限查看公文
    $.ajax({
        type:'POST',
        url:'../document/sent/detail/'+ doc_id,
        contentType: 'application/json;',
        dataType:'JSON',
        success:function (json,status) {
            if (json.err_msg == null) {
                var approvalList = json.data.approvalList;
                for (var i = 0; i < approvalList.length; i++) {
                    $("#dataTableBody").append("<tr>" +
                        "<td>" + approvalList[i].name + "</td>" +
                        "<td>" + approvalList[i].approval + "</td>" +
                        "<td>" + approvalList[i].option + "</td>" +
                        "</tr>");
                }
                //调用方法获得参数
                if(json.data.content!=null){
                    $("#documentIframe").css("display", "block");
                    iFrameHeight();
                    $("#documentIframe").attr("src", "../assets/js/document/pdfjs-dist/web/viewer.html?file=/document/draft/read/" + doc_id);
                }else{
                    $("#draft_pdf").css("display","none");
                }
            } else {
                alert(json.err_msg);
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {

        },
        fail:function () { //如果在传输过程失败了：调用fail中对应的匿名函数(可选)
            alert("您没有相关权限阅读公文!");
        },
        cache:false
    });

    // 获取所有附件列表
    $.ajax({
        dataType:'JSON',
        type: 'POST',
        url: '../document/review/annex/list/' + doc_id,
        dataType: 'JSON',
        success: function (json,status){
            //console.log(json);
            if(json.err_msg != null){
                alert(json.err_msg);
            }else{
                var documentAnnex = json.data.documentAnnex;
                for(var i = 0;i<documentAnnex.length;i++){
                    $("#annexTable").append("<tr>" +
                        "<td>"+documentAnnex[i].name+"</td>" +
                        "<td>"+documentAnnex[i].writer+"</td>" +
                        "<td><button type='button' class='btn btn-success' onclick='downFujian("+ documentAnnex[i].id +")'>下载</button>" +
                        "</td>");
                }
                //console.log(json.data.documentAnnex);d
            }
        },
        error: function (XMLHttpRequest, textStatus) {
            console.log(textStatus);
        },
        fail: function () {
            console.log("fail!");
        }
    });

    // 公文阅读状态
    $.ajax({
        dataType:'JSON',
        type: 'POST',
        url: '../document/sent/read_detail/' + doc_id,
        dataType: 'JSON',
        success: function (json,status){
            if(json.err_msg != null){
                alert(json.err_msg);
            }else{
                var documents = json.data.documents;
                // console.log(json.data);
                for(var i = 0;i<documents.length;i++){
                    $("#read-situation").append("<tr>" +
                        "<td>"+documents[i].account+"</td>" +
                        "<td>"+documents[i].name+"</td>" +
                        "<td>"+documents[i].already_read+"</td>" +
                        "</tr>"
                    );
                }
                //console.log(json.data.documentAnnex);d
            }
        },
        error: function (XMLHttpRequest, textStatus) {
            console.log(textStatus);
        },
        fail: function () {
            console.log("fail!");
        }
    });

    //iframe自适应
    function iFrameHeight() {
        var ifm= document.getElementById("documentIframe");
        var subWeb = document.frames ? document.frames["documentIframe"].document : ifm.contentDocument;
        if(ifm != null && subWeb != null) {
            //ifm.height = subWeb.body.scrollHeight;
            ifm.width = subWeb.body.scrollWidth;
        }
    }

    // 公文下载
    $("#documentDown").click(function () {
        //console.log
        window.location.href='../document/draft/down/' + doc_id;
    });

    
    // 公文删除
    $("#Delete_document").click(function () {
        if(confirm("是否要删除该记录?")){
            $.ajax({
                type:'DELETE',
                url:'../document/sent/delete/'+ doc_id,
                contentType: 'application/json;',
                dataType:'JSON',
                success:function (json,status) {
                    if(json.err_msg!=null){
                        alert(json.err_msg);
                    }else {
                        window.location.href = "../document/my_sent_doc.html";
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {

                },
                fail:function () { //如果在传输过程失败了：调用fail中对应的匿名函数(可选)
                    alert("fail!");
                },
                cache:false,
            });
        }
    });
    
    // 强制发送
    $("#forcedSend").click(function () {
        if(confirm("是否强制发送?")){
            $.ajax({
                type:'GET',
                url:'../document/sent/forced/'+ doc_id,
                contentType: 'application/json;',
                dataType:'JSON',
                success:function (json,status) {
                    if(json.err_msg!=null){
                        alert(json.err_msg);
                    }else {
                        window.location.href = "../document/my_sent_doc.html";
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {

                },
                fail:function () { //如果在传输过程失败了：调用fail中对应的匿名函数(可选)
                    alert("fail!");
                },
                cache:false,
            });
        }
    })
});

//downFujian
function downFujian(id) {
    window.location.href="../document/annex/down/" + doc_id + "/" + id;
}